package com.feature.core.mybatis;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feature.core.domain.PageVo;
import com.feature.core.util.MyBatisPlusUtil;

public interface SmartBaseMapper<T> extends BaseMapper<T> {
    
    default List<T> selectList() {
        return this.selectList(Wrappers.emptyWrapper());
    }

    default List<T> selectList(IQuery query) {
        return this.selectList(MyBatisPlusUtil.buildQueryWrapper(query));
    }

    default boolean exists(IQuery query) {
        return this.exists(MyBatisPlusUtil.buildQueryWrapper(query));
    }

    default Long selectCount(IQuery query) {
        return this.selectCount(MyBatisPlusUtil.buildQueryWrapper(query));
    }

    default PageVo<T> selectPageList(long curPage, long pageSize, IQuery query) {
        Page<T> page = Page.of(curPage, pageSize);
        List<T> list = this.selectList(page, MyBatisPlusUtil.buildQueryWrapper(query));
        PageVo<T> res = new PageVo<T>();
        res.setCurPage(curPage);
        res.setItems(list);
        res.setPageCount(page.getPages());
        res.setPageSize(pageSize);
        res.setTotalCount(page.getTotal());
        return res;
    }

    default List<T> selectList(T query) {
        return this.selectList(new QueryWrapper<T>(query));
    }

    default boolean exists(T query) {
        return this.exists(new QueryWrapper<T>(query));
    }

    default Long selectCount(T query) {
        return this.selectCount(new QueryWrapper<T>(query));
    }

    default PageVo<T> selectPageList(long curPage, long pageSize, T query) {
        Page<T> page = Page.of(curPage, pageSize);
        List<T> list = this.selectList(page, new QueryWrapper<T>(query));
        PageVo<T> res = new PageVo<T>();
        res.setCurPage(curPage);
        res.setItems(list);
        res.setPageCount(page.getPages());
        res.setPageSize(pageSize);
        res.setTotalCount(page.getTotal());
        return res;
    }

    default T getOne(T query, boolean throwEx) {
        return this.selectOne(Wrappers.query(query), throwEx);
    }

    default T getOne(T query) {
        return getOne(query, false);
    }
    
    default int deleteByEntity(T entity) {
        return this.delete(Wrappers.query(entity));
    }

}
